//Xylophone
//Adapted for an ArduinoMega
//from Jenna deBoisblanc and Spiekenzie Labs initial code
//*******************************************************************************************************************
// User settable variables
//*******************************************************************************************************************
intpinRead;
charpinAssignments[6]={
'A0','A1','A2','A3','A4','A5','A6','A7','A8','A9','A10','A11'};
bytePadNote[16]={
57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72}; // MIDI notes from 0 to 127 (Mid C = 60)
intPadCutOff[16]=
{
400,400,200,800,400,400,400,400,400,400,400,400,400,400,400,400}; // Minimum Analog value to cause a drum hit
intMaxPlayTime[16]={
90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90}; // Cycles before a 2nd hit is allowed
#define midichannel 1; // MIDI channel from 0 to 15 (+1 in "real world")
booleanVelocityFlag =true; // Velocity ON (true) or OFF (false)
//*******************************************************************************************************************
// Internal Use Variables
//*******************************************************************************************************************
booleanactivePad[16]={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Array of flags of pad currently playing
intPinPlayTime[16]={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Counter since pad started to play
bytestatus1;
intpin=0;
inthitavg=0;
//*******************************************************************************************************************
// Setup
//*******************************************************************************************************************
voidsetup()
{
Serial.begin(57600); // connect to the serial port 115200
}
//*******************************************************************************************************************
// Main Program
//*******************************************************************************************************************
voidloop()
{
for(intpin=0;pin<16;pin++) //
{
//int pin = 3;
// for (pinRead=0; pinRead < 16, pin++){
hitavg=analogRead(pinAssignments[pin]);
//Serial.println(hitavg);
// read the input pin
if((hitavg>PadCutOff[pin]))
{
if((activePad[pin]==false))
{
if(VelocityFlag==true)
{
// hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin])); // With full range (Too sensitive ?)
hitavg=(hitavg/8)-1; // Upper range
}
else
{
hitavg=127;
}
MIDI_TX(144,PadNote[pin],hitavg);//note on
PinPlayTime[pin]=0;
activePad[pin]=true;
}
else
{
PinPlayTime[pin]=PinPlayTime[pin]+1;
}
}
elseif((activePad[pin]==true))
{
PinPlayTime[pin]=PinPlayTime[pin]+1;
if(PinPlayTime[pin]>MaxPlayTime[pin])
{
activePad[pin]=false;
MIDI_TX(144,PadNote[pin],0);
}
}
}
}
//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
voidMIDI_TX(byteMESSAGE,bytePITCH,byteVELOCITY)
{
status1=MESSAGE+midichannel;
Serial.write(status1);
Serial.write(PITCH);
Serial.write(VELOCITY);
}